<template>
  <div class="home-container">
    <van-nav-bar fixed>
      <template #title>
        <van-button
          class="search-btn"
          slot="title"
          type="info"
          size="small"
          round
          icon="search"
          to="/search"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>
    <!-- 导航页签 -->
    <van-tabs v-model="active" class="channel-tabs" animated swipeable>
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <ArticleList :channel="item"></ArticleList>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div slot="nav-right" class="hamburger-btn" @click="popupShow = true">
        <i class="iconfont icon-gengduo"></i>
      </div>
    </van-tabs>
    <!-- 弹出层 -->
    <van-popup
      class="edit-channel-popup"
      v-model="popupShow"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '90%' }"
      ><ChannelEdit
        :active="active"
        :myChannels="channels"
        @update-active="onUpdateActive"
      ></ChannelEdit
    ></van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/articleList.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storeage.js'
export default {
  name: 'HomePage',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      // 频道数据数组
      channels: [],
      popupShow: false
    }
  },
  computed: {
    ...mapState(['user'])
  },
  watch: {},
  created () {
    this.loadChannels()
  },
  mounted () {},
  methods: {
    async loadChannels () {
      try {
        let channels = []
        const localChannels = getItem('TOUTIAO_CHANNELS')
        if (this.user || !localChannels) {
          const { data } = await getUserChannels()
          this.channels = data?.data?.channels
          return false
        } else {
          channels = localChannels
        }
        // 最终将拿到的频道列表给到
        this.channels = channels
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    // 编辑跳转事件
    onUpdateActive (i, isShow) {
      this.active = i
      this.popupShow = isShow
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-bottom: 100px;
  padding-top: 180px;
}
/deep/.van-tabs__wrap {
  position: fixed;
  top: 92px;
  z-index: 2;
  width: 100%;
}
.van-nav-bar {
  background-color: #3296fa;
}
/deep/.van-nav-bar__title {
  max-width: unset;
}
.search-btn {
  width: 555px;
  height: 64px;
  background-color: #5babfb;
  border: none;
  font-size: 22px;
  .van-icon {
    font-size: 36px;
  }
}
/deep/.channel-tabs {
  .van-tab {
    border-right: 1px solid #edeff3;
    min-width: 200px;
    height: 80px;
    .van-tab__text {
      font-size: 27px;
      color: #777777;
    }
  }
  .van-tab--active .van-tab__text {
    color: #333 !important;
  }
  .van-tabs__line {
    width: 31px !important;
    height: 6px;
    background: rgba(50, 150, 250, 1);
    border-radius: 3px;
    bottom: 8px;
  }
  .van-tabs__nav {
    padding: 0;
  }
  .placeholder {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
  }
  .hamburger-btn {
    position: fixed;
    right: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 66px;
    height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao {
      font-size: 33px;
    }
    &:before {
      content: '';
      position: absolute;
      left: 0;
      width: 1px;
      height: 100%;
      background-image: url(~@/assets/image/gradient-gray-line.png);
      background-size: contain;
    }
  }
}
.edit-channel-popup {
  padding-top: 100px;
  box-sizing: border-box;
}
</style>
